jsx3.Class.defineClass("ux.cdb.admin.gui.MatrixSearchBar", jsx3.gui.Block, null, 
    function(controller, controller_prototype) {

        controller.DO_QUERY = "ux.cdb.admin.gui.MatrixSearchBar.DO_QUERY";

        controller.prototype.init = function(strName,vntLeft,vntTop,vntWidth,vntHeight,strHTML) {
            this.jsxsuper(strName,vntLeft,vntTop,vntWidth,vntHeight,strHTML);
            this.initialized = false;
        };

        controller.prototype.onAfterPaint = function() {
            if(this.targetMatrixName) { this.setTargetMatrixName(this.targetMatrixName); };
            if(!this.initialized) this.initialize();
        };

        controller.prototype.onSetParent = function(parent) {
            this.initialize();
            return this.jsxsuper(parent);
        };

        controller.prototype.initialize = function() {
            var me = this;
            this.targetObject = null;
            this.fields = this.getFields();
            this.conditions = this.getConditions();
            this.keyword = this.getKeyword();
            this.btnSearch = this.getButton();
            this.initializeConditions();

            this.btnSearch.enable = function() {
                this.setEnabled(jsx3.gui.Form.STATEENABLED, true);
                return this;
            };
            this.btnSearch.disable = function() {
                this.setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                return this;
            };
            this.btnSearch.doQuery = function() {
                me.doQuery.call(me);
            };

            this.btnSearch.disable();

            this.fields.doSelect = function() {me.reStateButton.call(me)};
            this.conditions.doSelect = function() {me.reStateButton.call(me)};
            this.keyword.doKeyPress = function() {me.reStateButton.call(me)};

            this.initialized = true;
        };

        controller.prototype.setTargetMatrixName = function(matrixName) {
            if(!this.initialized) this.initialize();
            if(typeof(matrixName) == "string") {
                this.targetMatrixName = matrixName;
                this.targetObject = ux.megatron.Main.getServer().getJSXByName(this.targetMatrixName);
                if(!this.targetObject) {
                    try {
                        var vars = new Object();
                        vars["objJSX"] = this;
                        this.targetObject = jsx3.eval(this.targetMatrixName, vars);
                    }catch(ex) {
                        ux.megatron.Main.logException(ex);
                    }
                }
                if(!this.targetObject || !(this.targetObject instanceof jsx3.gui.Matrix)) {
                    throw new ux.megatron.exception.InvalidTargetException("Specified target is not an Matrix Name");
                    return false;
                }
            } else if(typeof(matrixName) == "object" && matrixName instanceof jsx3.gui.Matrix) {
                this.targetMatrixName = matrixName.getName();
                this.targetObject = matrixName;
            } else {
                throw new ux.megatron.exception.InvalidTargetException("Specified target is not an Matrix or Matrix Name");
                return false;
            }

            this.initializeFields();
        };

        controller.prototype.getTargetMatrixName = function() {
            return this.targetMatrixName;
        };

        controller.prototype.reStateButton = function() {
            if(this.validateFilling()) {
                this.btnSearch.enable();
            } else {
               this.btnSearch.disable();
            }
        };

        controller.prototype.validateFilling = function() {
            if(!this.fields.getValue()) return false;
            if(!this.conditions.getValue()) return false;
            if(!this.keyword.getValue()) return false;
            return true;
        };

        controller.prototype.initializeFields = function() {
            var tmpCols = this.targetObject.getChildren();
            this.fields.getXML().removeChildren();
            for (var i=0; i<tmpCols.length; i++) {
                var id = tmpCols[i].getId();
                var record = this.fields.getXML().createNode(jsx3.xml.Entity.TYPEELEMENT, "record", "");
                var colText = this.getColumnText(tmpCols[i]);
                if(colText && (colText != "")) {
                    record.setAttribute("id", id);
                    record.setAttribute("jsxtext", colText);
                    record.setAttribute("jsxid", tmpCols[i].getPath());
                    this.fields.getXML().appendChild(record);
                }
           }
           this.reStateButton();
        };

        controller.prototype.initializeConditions = function() {
            this.conditions.getXML().removeChildren();
            var srcPath = ux.megatron.Main.getServer().resolveURI("jsxaddin://user!cdbadmin/xml/Conditions.xml");
            this.conditions.setXMLURL(srcPath, true);
        };

        controller.prototype.doQuery = function(btnObj) {
            var msg = new Object();
            msg.subject = ux.cdb.admin.gui.MatrixSearchBar.DO_QUERY;
            msg.target = this;
            msg.field = this.getFields().getValue();
            msg.condition = this.getConditions().getValue();
            msg.keyword = this.getKeyword().getValue();
            //debug
            alert("Field: " + msg.field + "; Conditon:" + msg.condition + "; KeyWord=" + msg.keyword);
            try {
                this.publish(msg);
            }
            catch(ex2) {
                ux.megatron.Main.logException(ex2)
            }
        };

        controller.prototype.getColumnText = function(col) {
            var text = col.getText();
            if(!text) {
                var dp = col.getDynamicProperty("jsxtext");
                if(dp) {
                    text = ux.megatron.Main.getServer().getDynamicProperty(dp);
                    if(!text) {
                        text = "";
                    }
                }
            }
            return text;
        };

        controller.prototype.getFields = function() {
            return this.getChild(0).getChild(1);
        };

        controller.prototype.getConditions = function() {
            return this.getChild(1).getChild(1);
        };

        controller.prototype.getKeyword = function() {
            return this.getChild(2).getChild(1);
        };

        controller.prototype.getButton = function() {
            return this.getChild(3).getChild(0);
        };
    });